package cn.edu.gdu.quizpractice.dao;

import cn.edu.gdu.quizpractice.entity.Bank;
import cn.edu.gdu.quizpractice.entity.Category;
import cn.edu.gdu.quizpractice.entity.ChoiceQuestion;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author: Aubery
 * @Date: 2025-02-25-22:12
 * @Description:
 */

@Mapper
public interface CategoryDao extends BaseMapper<Category> {

    @Insert("insert into t_categories(category_name) values (#{categoryName})")
    @Options(useGeneratedKeys = true, keyProperty = "categoryId", keyColumn = "category_id")
    boolean insertCategory(Category category);


    @Select("select * from t_categories where category_name = #{name}")
    @Results({
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "categoryName", column = "category_name"),
            @Result(property = "questions", column = "category_id",
                    many = @Many(select = "findQuestionsByCategoryId"))
    })
    Category findByName(String name);

    /*@Select("SELECT " +
            "c.category_id AS categoryId, " +
            "c.category_name AS categoryName, " +
            "q.cquestion_id AS questionId, " +
            "q.cq_content AS content, " +
            "q.option_a AS optionA, " +
            "q.option_b AS optionB, " +
            "q.option_c AS optionC, " +
            "q.option_d AS optionD, " +
            "q.correct_answer AS answer, " +
            "q.analysis AS analysis, " +
            "q.difficulty AS difficulty, " +
            "q.is_reviewed AS isReviewed, " +
            "q.is_multiple AS isMultiple, " +
            "q.created_at AS createTime, " +
            "q.updated_at AS updateTime " +
            "FROM t_categories c " +
            "LEFT JOIN t_choicequestions q ON c.category_id = q.category_id " +
            "ORDER BY q.category_id")
    @Results({
            @Result(property = "categoryId", column = "categoryId"),
            @Result(property = "categoryName", column = "categoryName"),
            @Result(property = "questions", column = "categoryId",
                    many = @Many(select = "findQuestionsByCategoryId"))
    })
    List<Category> findAll();*/

    /*@Select("SELECT * FROM t_choicequestions WHERE category_id = #{categoryId}")
    @Results({
            @Result(property = "cquestionId", column = "cquestion_id"),
            @Result(property = "categoryId", column = "category_id"),
            @Result(property = "content", column = "cq_content"),
            @Result(property = "optionA", column = "option_a"),
            @Result(property = "optionB", column = "option_b"),
            @Result(property = "optionC", column = "option_c"),
            @Result(property = "optionD", column = "option_d"),
            @Result(property = "answer", column = "correct_answer"),
            @Result(property = "analysis", column = "analysis"),
            @Result(property = "difficulty", column = "difficulty"),
            @Result(property = "isReviewed", column = "is_reviewed"),
            @Result(property = "isMultiple", column = "is_multiple"),
            @Result(property = "createTime", column = "created_at"),
            @Result(property = "updateTime", column = "updated_at")
    })
    List<ChoiceQuestion> findQuestionsByCategoryId(Integer categoryId);*/

    @Delete("DELETE FROM t_categories WHERE category_name = #{name}")
    boolean deleteByName(String name);


    @Select("SELECT * FROM t_categories order by category_id")
    List<Category> findAll();

    @Insert("insert into t_categories(category_name) values (#{categoryName})")
    boolean addCateory(String categoryName);

    @Select("SELECT DISTINCT b_name\n" +
            "FROM t_choicequestions\n" +
            "WHERE category_name = #{categoryName}")
    List<String> findBanksByCategoryName(String categoryName);


}
